package com.huaxin.common.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.huaxin.common.R;

/**
 * Created by Administrator on 2016/12/28.
 * 方形
 */

public class RoundLayout extends FrameLayout {

    private final Paint boundPaint;
    private final int stockWidth;
    private float topLeftRadius;
    private float topRightRadius;
    private float bottomLeftRadius;
    private float bottomRightRadius;

    private Paint roundPaint;
    private Paint imagePaint;

    public RoundLayout(Context context) {
        this(context, null);
    }

    public RoundLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundLayout);
            float radius = ta.getDimension(R.styleable.RoundLayout_radius, 0);
            topLeftRadius = ta.getDimension(R.styleable.RoundLayout_topLeftRadius, radius);
            topRightRadius = ta.getDimension(R.styleable.RoundLayout_topRightRadius, radius);
            bottomLeftRadius = ta.getDimension(R.styleable.RoundLayout_bottomLeftRadius, radius);
            bottomRightRadius = ta.getDimension(R.styleable.RoundLayout_bottomRightRadius, radius);
            ta.recycle();
        }
        roundPaint = new Paint();
        roundPaint.setColor(Color.WHITE);
        roundPaint.setAntiAlias(true);
        roundPaint.setStyle(Paint.Style.FILL);
        roundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        imagePaint = new Paint();
        imagePaint.setXfermode(null);

        stockWidth = 2;
        boundPaint = new Paint();
        boundPaint.setAntiAlias(true);
        boundPaint.setColor(Color.argb(128,0,0,0));
        boundPaint.setStrokeWidth(stockWidth);
        boundPaint.setStyle(Paint.Style.STROKE);
    }
    ////实现1  无法消除锯齿
//    @Override
//    protected void dispatchDraw(Canvas canvas) {
//        int width = getWidth();
//        int height = getHeight();
//        Path path = new Path();
//        path.moveTo(0, topLeftRadius);
//        path.arcTo(new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2), -180, 90);
//        path.lineTo(width - topRightRadius, 0);
//        path.arcTo(new RectF(width - 2 * topRightRadius, 0, width, topRightRadius * 2), -90, 90);
//        path.lineTo(width, height - bottomRightRadius);
//        path.arcTo(new RectF(width - 2 * bottomRightRadius, height - 2 * bottomRightRadius, width, height), 0, 90);
//        path.lineTo(bottomLeftRadius, height);
//        path.arcTo(new RectF(0, height - 2 * bottomLeftRadius, bottomLeftRadius * 2, height), 90, 90);
//        path.close();
//        canvas.clipPath(path);
//        super.dispatchDraw(canvas);
//    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        canvas.saveLayer(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), imagePaint, Canvas.ALL_SAVE_FLAG);
        super.dispatchDraw(canvas);
        drawTopLeft(canvas);
        drawTopRight(canvas);
        drawBottomLeft(canvas);
        drawBottomRight(canvas);
        drawBound(canvas);
        canvas.save();
        canvas.restore();
    }

    private void drawBound(Canvas canvas) {
        canvas.restore();
        canvas.translate(stockWidth/2,stockWidth/2);
        int width = canvas.getWidth() - stockWidth;
        int height = canvas.getHeight() - stockWidth;
        float tl = topLeftRadius - stockWidth/2;
        float tr = topRightRadius - stockWidth/2;
        float bl = bottomLeftRadius -stockWidth/2;
        float br = bottomRightRadius - stockWidth/2;

        Path path = new Path();
        path.moveTo(0, tl);
        path.arcTo(new RectF(0, 0, tl * 2, tl * 2),
                180, 90);
        path.lineTo(width  - tr,0);
        path.arcTo(new RectF(width - tr * 2, 0, width, tr *2),
                -90, 90 );
        path.lineTo(width,height-bl);
        path.arcTo(new RectF(width - 2 * bl, height - 2 * bl, width, height),
                0, 90);
        path.lineTo(br,height);
        path.arcTo(new RectF(0, height - 2 * br,
                        br * 2, height),
                90, 90);
        path.lineTo(0, tl);
        path.close();
        canvas.drawPath(path, boundPaint);
        canvas.save();
        canvas.restore();
    }

    private void drawTopLeft(Canvas canvas) {
        if (topLeftRadius > 0) {
            Path path = new Path();
            path.moveTo(0, topLeftRadius);
            path.lineTo(0, 0);
            path.lineTo(topLeftRadius, 0);
            path.arcTo(new RectF(0, 0, topLeftRadius * 2, topLeftRadius * 2),
                    -90, -90);
            path.close();
            canvas.drawPath(path, roundPaint);
        }
    }

    private void drawTopRight(Canvas canvas) {
        if (topRightRadius > 0) {
            int width = getWidth();
            Path path = new Path();
            path.moveTo(width - topRightRadius, 0);
            path.lineTo(width, 0);
            path.lineTo(width, topRightRadius);
            path.arcTo(new RectF(width - 2 * topRightRadius, 0, width,
                    topRightRadius * 2), 0, -90);
            path.close();
            canvas.drawPath(path, roundPaint);
        }
    }

    private void drawBottomLeft(Canvas canvas) {
        if (bottomLeftRadius > 0) {
            int height = getHeight();
            Path path = new Path();
            path.moveTo(0, height - bottomLeftRadius);
            path.lineTo(0, height);
            path.lineTo(bottomLeftRadius, height);
            path.arcTo(new RectF(0, height - 2 * bottomLeftRadius,
                    bottomLeftRadius * 2, height), 90, 90);
            path.close();
            canvas.drawPath(path, roundPaint);
        }
    }

    private void drawBottomRight(Canvas canvas) {
        if (bottomRightRadius > 0) {
            int height = getHeight();
            int width = getWidth();
            Path path = new Path();
            path.moveTo(width - bottomRightRadius, height);
            path.lineTo(width, height);
            path.lineTo(width, height - bottomRightRadius);
            path.arcTo(new RectF(width - 2 * bottomRightRadius, height - 2
                    * bottomRightRadius, width, height), 0, 90);
            path.close();
            canvas.drawPath(path, roundPaint);
        }
    }

}
